package com.learn.springboot.controller;

import com.learn.springboot.common.R;
import com.learn.springboot.param.StudentParam;
import com.learn.springboot.util.ValidateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.NotNull;

/**
 * Hello控制器
 *
 * @author Don
 * @date 2020/07/08
 */
@Slf4j
@Validated
@RestController
public class HelloController {
    @GetMapping("/student")
    public R<String> get(@NotNull(message = "name 不能为空") String name,
                         @Max(value = 99, message = "不能大于99岁") Integer age) {
        log.info("HelloController get ...");
        return R.ok();
    }

    @PostMapping("/student")
    public R<String> save(@Valid @RequestBody StudentParam studentParam) {
        log.info("HelloController save ...");
        return R.ok();
    }

    @GetMapping("/student2")
    public R<String> save2(@Valid StudentParam studentParam) {
        log.info("HelloController save2 ...");
        return R.ok();
    }

    @GetMapping("/student3")
    public R<String> save3(StudentParam studentParam) {
        log.info("HelloController save3 ...");
        ValidateUtil.validate(studentParam);
        return R.ok();
    }
}
